package com.lzz.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhenzhen
 * @date 2018-12-17 上午10:45
 */
@Slf4j
public class MyHandlerInterceptor1 implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * preHandle返回false或者跑出异常就不会继续向下执行
         * 即不会执行controller层，也不会执行posthandle和afterCompletion
         */
        log.info("========[MyHandlerInterceptor1:] 调用controller之前========");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        /**
         * 调用Controller之后视图渲染完成之前执行
         */
        log.info("========[MyHandlerInterceptor1:] 调用controller之后========");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        /**
         * 视图渲染完成之后执行
         */
        log.info("========[MyHandlerInterceptor1:] 渲染视图========");
    }
}
